package cn.befory.web;

import cn.befory.common.Result;
import cn.befory.service.UrlService;
import cn.befory.service.model.GenerateUrlParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Size;

@RequestMapping("/url")
@RestController
@Api("链接管理")
@Validated
public class UrlController {
    @Autowired
    private UrlService urlService;


    @ApiOperation(value = "生成短链接",notes = "生成短链接")
    @PostMapping("/generate")
    public Result<String> generateShortUrl(@Valid @RequestBody GenerateUrlParam generateUrlParam){
        return Result.ok(urlService.generateShortUrl(generateUrlParam));
    }


    @ApiOperation(value = "获取长连接",notes = "通过短链接获取长连接")
    @ApiImplicitParam(name = "shortUrl", value = "短链接", required = true, paramType = "query", dataType = "string")
    @GetMapping("/long")
    public Result<String> getLongUrl(@Size(min = 1,max = 8) @RequestParam(name = "shortUrl") String shortUrl){
        return Result.ok(urlService.getLongUrl(shortUrl));
    }
}
